//
//  NavigationBar.swift
//  EasyDana
//
//  Created by mac on 2024/10/11.
//

import UIKit
import RxSwift
import RxCocoa
class NavigationBar: UIView {

    let bag = DisposeBag()
    
    @IBInspectable
    var title: String? {
        set {
            self.titleLabel.text = newValue
        }
        
        get {
            return titleLabel.text
        }
    }
    
    @IBInspectable
    var titleColor: UIColor? {
        set {
            self.titleLabel.textColor = newValue
        }
        get {
            return self.titleLabel.textColor
        }
    }
    
    @IBInspectable
    var backColor: UIColor? {
        get {
            return backButton.tintColor
        }
        set{
            backButton.tintColor = newValue
        }
    }
    
    @IBInspectable
    var backHidden: Bool {
        get {
            return backButton.isHidden
        }
        set {
            backButton.isHidden = newValue
        }
    }
    
    private lazy var backButton: UIButton = {
        let btn = UIButton(type: .system)
        btn.setImage(UIImage(named: "navi_back")?.withRenderingMode(.alwaysTemplate), for: .normal)
        return btn
    }()
    
    private lazy var titleLabel: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont(name: "Righteous-Regular", size: 16)
        return lbl
    }()
    
    let backClicked = PublishSubject<Void>.init()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.configView()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.configView()
    }
    
    
    private func configView() {
        self.addSubview(self.titleLabel)
        self.titleLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        self.addSubview(self.backButton)
        self.backButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(15)
        }
        self.backButton.rx.tap
            .bind(to: self.backClicked)
            .disposed(by: self.bag)
    }
}
